/**
 * Created by dukes on 2016/2/29.
 */
class GuildListPanel extends eui.Component{
    private curIndex = 0;
    public constructor(){
        super();
        this.skinName = "skins.scene.GuildListPanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addedToStage, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
    }

    private onCreate(evt:egret.TouchEvent){
        if( Model.me.vipLevel < 2){
            ShowHint("vip2才能创建家族哦");
            return;
        }
        CreateGuildPanel.getInstance().show();
    }

    private onSearch(evt:egret.TouchEvent){
        SearchGuildPanel.getInstance().show();
    }
    public onLeft(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curIndex--;
        this.updatePageDisplay();
    }
    public onRight(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curIndex++;
        this.updatePageDisplay();
    }

    private updatePageDisplay(){
        if( this.curIndex<1){
            this.curIndex = 1;
        }
        this.img_page_up.visible = this.curIndex>1;
        window["__guildList"](this.curIndex,0);
    }

    public onApplyBack(){
        //var arr = this.provider.source;
        //if( !arr || arr.length == 0 )
        //    return;
        //var len = arr.length;
        //var data;
        //for(var i = 0; i < len; i++){
        //    data = arr[i];
            this.provider.refresh()
        //}
    }

    public onGuildListBack(list){
        this.provider.source = list;
        this.img_page_down.visible = list.length == 8;
    }

    private addedToStage(evt:egret.Event){
        if( this.curIndex == 0){
            this.curIndex = 1;
        }
        this.updatePageDisplay();
        this.img_create.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCreate, this);
        this.img_search.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSearch, this);
        this.img_page_up.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_page_down.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }

    private removedFromStage(evt:egret.Event){
        this.img_create.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCreate, this);
        this.img_search.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSearch, this);
        this.img_page_up.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_page_down.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }

    private provider:eui.ArrayCollection;

    public guild_list:eui.List;
    public img_page_up:eui.Image;
    public img_page_down:eui.Image;
    public img_create:eui.Image;
    public img_search:eui.Image;

    public partAdded(name:string,instance:any){
        super.partAdded(name,instance);
        if( name == "guild_list"){
            this.guild_list = instance;
            this.guild_list.itemRenderer = GuildListRender;
            this.provider = new eui.ArrayCollection();
            this.guild_list.dataProvider = this.provider;
        }else if( name == "img_page_up"){
            this.img_page_up = instance;
        }else if( name == "img_page_down"){
            this.img_page_down = instance;
        }else if( name == "img_create"){
            this.img_create = instance;
        }else if( name == "img_search"){
            this.img_search = instance;
        }
    }

    private static _instance:GuildListPanel;
    public static getInstance():GuildListPanel{
        if( GuildListPanel._instance == null)
            GuildListPanel._instance = new GuildListPanel();
        return GuildListPanel._instance;
    }
}